<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * 添加一个相应相册的上传图片的新鲜事.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_action.php';
require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/lib/slh_rights.php';

require_once INSTALLDIR . '/widgets/slh_form_addphotonews.php';

require_once INSTALLDIR . '/classes/PhotoNews.php';
require_once INSTALLDIR . '/classes/PhotoSet.php';

class AddphotonewsAction extends Action
{
	/**
	 * The id of the photoset.
	 */
	var $photoset_id = null;
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_NEWS)) {
			return false;
		}
		
		parent::prepare($args);
		
		// Get the photoset_id.
		$this->photoset_id = $this->getTrimmedArg('pphotoset_id');
		
		if (empty($this->photoset_id)) {
			$this->clientError('没有这个相册呃.');
		} else if ( !PhotoSet::hasId($this->photoset_id)) {
			$this->clientError('没有找到这个相册呃.');
		}
		
		return true;
	}
	
	function handle($args)
	{
		parent::handle($args);
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$submit = $this->getTrimmedArg('submit');
			if ('完成返回' == $submit) {
				$this->goBack();
			} else if ('添加新鲜事' == $submit) {
				$this->tryAdd();
			}
		} else {
			;
		}
	}
	
// Overrided ends //////////////////////////////////////////////////////////////

	/**
	 * Try to add the data into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		// Check the post interval.
		if ( !PhotoNews::canPostWithinInterval($this->photoset_id)) {
			$this->clientError('您已经发布了该相册的新鲜事了.');
		}

		$user_id = slh_get_userid();
		
		$photonews_id = PhotoNews::add(array('photoset_id' => $this->photoset_id,
											 'user_id' => $user_id));
		
		if ( !$photonews_id) {
			$this->clientError('添加图片新鲜事失败!');
		}
		
		// Success.
		// Unset the seesion token preventing post-again.
		$this->unsetSessionToken();
		
		// Redirect to the photo set.
		slh_redirect(slh_action2url('photoset', array('gphotoset_id' => $this->photoset_id)), 303);
	}
	
	/**
	 * Go back to the photoset.
	 */
	function goBack()
	{
		// Redirect to the photo set.
		slh_redirect(slh_action2url('photoset', array('gphotoset_id' => $this->photoset_id)), 303);
	}
}

?>